﻿//
// Copyright (c) 2011 HyperCrunch, Inc. http://www.hypercrunch.com 
// Author: Nik Kalyani  http://www.kalyani.com (Twitter: @techbubble)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using HyperCrunch.Druid;
using HyperCrunch.Druid.RuleMatchers;
using System;

namespace HyperCrunch.Druid.ContentModifiers
{
    public class SetCookieContentModifier : IContentModifier
    {
        /// <summary>
        /// Sets a cookie in the user's browser if uiContext.JsonSettings contains
        /// keys whose name begins with "cookie". The values of these items is assumed 
        /// to contain properties Name, Value, Domain, Expires and Path
        /// </summary>
        /// <param name="uiContext"></param>
        /// <param name="page"></param>
        public void RenderContent(UIContext uiContext, Page page)
        {
            RuleSettings settings = uiContext.JsonSettings;
            foreach (string key in settings.Keys)
            {
                if (key.ToLower().StartsWith("cookie"))
                {
                    Dictionary<string, object> cookie = (Dictionary<string, object>)settings[key];
                    if ((cookie.ContainsKey("Name")) && (cookie.ContainsKey("Value")))
                    {
                        HttpCookie httpCookie = new HttpCookie(cookie["Name"].ToString(), cookie["Value"].ToString());

                        if (cookie.ContainsKey("Domain")) httpCookie.Domain = cookie["Domain"].ToString();
                        if (cookie.ContainsKey("Path")) httpCookie.Domain = cookie["Path"].ToString();
                        if (cookie.ContainsKey("Expires")) httpCookie.Expires = DateTime.Parse(cookie["Expires"].ToString());

                        uiContext.Context.Response.SetCookie(httpCookie);
                    }
                }
            }
        }
    }
}
